import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutterproject/mvvm/base/base_controller.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:get/get.dart';

class SmsCodeController extends BaseController {
  HttpClient client = Get.find<HttpClient>();

  /// type:1注册，2忘记密码，3转户，4注销账户，5修改密码，6修改支付密码
  Future<bool> sendCode({required String mobile, required int type}) async {
    if (mobile.length != 11) {
      ToastUtil.showFailed("请输入手机号");
      return false;
    }
    try {
      ;
      final timestamp = DateTime.now().millisecondsSinceEpoch;
      DialogUtil.showLoading();
      await client.get(AddressManager.sendCode, queryParameters: {
        "type": type,
        "mobile": mobile,
        "timestamp": timestamp,
        "signature": md5
            .convert(utf8.encode("CODE$mobile${timestamp}GuanYunWineApp"))
            .toString(),
      });
      ToastUtil.showSuccess("发送成功!");
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }
}
